package com.li.controller;


import com.li.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  1. @RequestBody:将请求体内容和形参进行绑定
 *  2. 使用@RequestBody注解将json格式的参数转化为pojo
 *      a> 导入jackson的依赖
 *      b> 在SpringMVC的配置文件中设置<mvc:annotation-driven />
 *      c> 在处理请求的控制器方法的形参位置，直接设置json格式的请求参数要转换的pojo的形参，使用@RequestBody注解标识即可
 *
 *  3. @ResponseBody:将所标识的控制器方法的返回值作为响应报文的响应体响应到浏览器
 *  4. 使用@ResponseBody注解响应浏览器json格式的数据
 *      a> 导入jackson的依赖
 *      b> 在SpringMVC的配置文件中设置<mvc:annotation-driven />
 *      c> 将需要转换为json字符串的java对象直接作为控制器方法的返回值，
 *          使用@ResponseBody注解控制器方法就可以将java对象直接转换为json字符串，并响应到浏览器
 */


@Controller
//@RestController //符合注解，相当于@Controller + @ResponseBody，为此类的每一个方法添加了@ResponseBody注解
public class TestAjaxController {

    @RequestMapping("/test/ajax")
    public void testAjax(Integer id, @RequestBody String requestBody, HttpServletResponse response) throws IOException {
        System.out.println("requestBody = " + requestBody);
        System.out.println("id = " + id);
        response.getWriter().write("hello, axios");
    }


    // 两种获取请求体中内容的方式
    // 1. 使用一个pojo与其对应
//    @RequestMapping("/test/RequestBody/json")
    public void testRequestBody(@RequestBody User user, HttpServletResponse response) throws IOException {
        response.getWriter().write("hello, RequestBody");
        System.out.println("user = " + user);
    }
    // 2. 使用map来对其所具有的参数一一映射
    @RequestMapping("/test/RequestBody/json")
    public void testRequestBody(@RequestBody Map<String, Object> map, HttpServletResponse response) throws IOException {
        response.getWriter().write("hello, RequestBody");
        System.out.println("map = " + map);
    }



    /*

        三种方式返回到ResponseBody中
        1. 直接返回pojo
        2. 返回Map
        3. 返回List

     */

//    @RequestMapping("/test/ResponseBody/json")
//    @ResponseBody
    public User testResponseBodyJson(){
        User user = new User(1001, "admin", "123455", 20, "男");
        return user;
    }

//    @RequestMapping("/test/ResponseBody/json")
//    @ResponseBody
    public Map<String, Object> testResponseBodyMap(){
        User user1 = new User(1001, "admin", "123455", 20, "男");
        User user2 = new User(1002, "admin", "123455", 20, "男");
        User user3 = new User(1003, "admin", "123455", 20, "男");
        Map<String, Object> map = new HashMap<>();
        map.put("1001", user1);
        map.put("1002", user2);
        map.put("1003", user3);
        return map;
    }

    @RequestMapping("/test/ResponseBody/json")
    @ResponseBody
    public List<User> testResponseBodyList(){
        User user1 = new User(1001, "admin", "123455", 20, "男");
        User user2 = new User(1002, "admin", "123455", 20, "男");
        User user3 = new User(1003, "admin", "123455", 20, "男");
        List<User> list = new ArrayList<User>();
        list.add(user1);
        list.add(user2);
        list.add(user3);
        return list;
    }

}
